/******************************************************************************

  File Name     : cpp_demo.cpp
  Version       : Initial Draft
  Author        : tongzhilin
  Created       : 2020/8/6
  Last Modified :
  Description   : cpp_demo
  Function List :
  History       :
  1.Date        : 2020/10/27
    Author      : tongzhilin
    Modification: Created file

******************************************************************************/
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <wchar.h>
#include <stddef.h>
#include <malloc.h>
#include <time.h>
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include <signal.h>
#include <stdint.h>

#include <pthread.h>
#include <semaphore.h>
#include <time.h>
#include <sys/time.h>
#include <sys/timeb.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/syscall.h>

#ifdef __cplusplus
#include <vector>
#include <map>
#include <memory>
#include <algorithm> 
#include <chrono>
#include <cmath>
#include <thread>
#include <mutex>
#include <unordered_map>
#include <climits>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <fstream> 
#include <cstddef>
#include <sstream>
#include <string>
#include <iterator>
#include <exception>
#include <stdexcept>
#endif
#include "rtsp_client_api.h"
#include "../src/mp_common.h"

int my_write264file(int chn, const char *filename, void* data, int size)
{
    int ret = -1;
    int writesize = 0;
    static FILE  *fp[16] = {NULL};
    if (!filename || !data || size == 0)
    {
        MP_LOG_ERR("input param is NULL\n");
        return -1;
    }

    if (!fp[chn])
    {
        fp[chn] = fopen(filename, "wb+");
        if(!fp[chn])
        {
            MP_LOG_ERR("fp is NULL, %s\n", filename);
            return -2;
        }
    }
    writesize = fwrite(data, 1, size, fp[chn]);
    if (writesize != size)
    {
        MP_LOG_ERR("fwrite err, %s\n", filename);
    }
    ret = writesize;
    return ret;
}

void CB_RtspVideoRecv(int _s32Chn, BL_FRAME_DATA_S* _pstFrameData)
{
    MP_LOG_DBG("recv video frame.chn : %d , len : %d, 264_265 : %d, I_P : %d, pts : %lld\n", _s32Chn, 
        _pstFrameData->m_s32DataSize, _pstFrameData->m_eEncodeType, _pstFrameData->m_eFrameType, _pstFrameData->m_u64Pts);
    static int s32ICount[16] = {0};
    std::string strFileName = "test_chn";
    strFileName += std::to_string(_s32Chn);
    strFileName += ".h264";
    if(_pstFrameData->m_eFrameType == BL_AVFRM_TYPE_I)
    {
        s32ICount[_s32Chn]++;
    }

    if(s32ICount[_s32Chn] >= 1)
    {
        //my_write264file(_s32Chn, strFileName.c_str(), _pstFrameData->m_pdata, _pstFrameData->m_s32DataSize);
    }
    return;
}

void CB_RtspAudioRecv(int _s32Chn, BL_AUDIO_DATA_S* _pstAudioData)
{
    MP_LOG_DBG("recv audio.chn : %d , len : %d, pts : %lld\n", _s32Chn, _pstAudioData->m_s32DataSize, _pstAudioData->m_u64Pts);
    return;
}

int main()
{
    RTSP_CLEINT_INIT_INFO_S stRtspInit;
    memset(&stRtspInit, 0, sizeof(stRtspInit));
    stRtspInit.m_pVideoCb = (void *)CB_RtspVideoRecv;
    stRtspInit.m_pAudioCb = (void *)CB_RtspAudioRecv;
    rtsp_init(stRtspInit);

    std::string strUrl = "rtsp://admin:123456@10.58.123.221:554";

    rtsp_open_url(0, strUrl.c_str());
    rtsp_open_url(1, strUrl.c_str());
    rtsp_open_url(2, strUrl.c_str());

    while(1)
    {
        sleep(1);
    }
    return 0;
}

